#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>
int main()
{
    int pipefd[2] = {0};
    if(pipe(pipefd) < 0)
    {
        perror("pipe:");
        exit(1);
    }    
    //以父进程读取，子进程写入为例子
    pid_t id = fork();
    if(id == 0)
    {
        const char* msg = "hello I am child";
        close(pipefd[0]);
        while(1)
            write(pipefd[1],msg,strlen(msg)),sleep(1);
        close(pipefd[1]);
        exit(0);
    }
    else if(id < 0)
    {
        perror("fork:");
        exit(2);
    }
    //parent
    close(pipefd[1]);
    char buf[64];
    while(1)
    {
        memset(buf,0,sizeof(buf));
        ssize_t n = read(pipefd[0],buf,sizeof(buf)-1);
        if(n > 0)
        {
            buf[n] = 0;
            printf("recevie:%s\n",buf);
        }
        else if(n == 0)
        {
            printf("对端关闭....\n");
            break;
        }
        else
        {
            perror("read:");
            exit(3);
        }
    }
    close(pipefd[0]);
    int status = 0;
    int ret = waitpid(-1,&status,0);
    printf("exitCode:%d exitSignal:%d,code dump:%d\n",(status >> 8)&0xff,(status&0x7f),(status >> 7)&1);
    return 0;
}